package com.peoit.honeycomb.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.peoit.honeycomb.application.UserApplication;
import com.peoit.honeycomb.domain.User;
import com.peoit.honeycomb.domain.exception.DuplicatedMobileException;
import com.peoit.honeycomb.domain.exception.DuplicatedUserNameException;
import com.peoit.honeycomb.dto.CommonResultJsonDto;
import com.peoit.honeycomb.dto.PageCommonResultDto;
import com.peoit.honeycomb.dto.UserDto;
import com.peoit.honeycomb.dto.UserQueryDto;

/**
 * Created by Bing.Zeng on 2015/9/24.
 */
@Controller
@RequestMapping("/admin/user")
public class UserController extends HoneycombBaseController {
	Logger logger = Logger.getLogger(ProjectController.class.getName());

	@Autowired
	UserApplication userApplication;

	@RequestMapping("/list/{offset}")
	public ModelAndView list(UserQueryDto dto,@PathVariable int offset) {
		dto.setOffset(offset);
		List<User> list = userApplication.findByWherePage(dto);
		List<UserDto> dtos = new ArrayList<>();
		for (User user : list) {
			UserDto userDto = UserDto.toDto(user);
			dtos.add(userDto);
		}

		Long count = userApplication.findUserCount(dto);

		PageCommonResultDto pageCommonResultDto = new PageCommonResultDto();
		pageCommonResultDto.setResult(dtos);
		pageCommonResultDto.setPageSize(dto.getPageSize());
		pageCommonResultDto.setCount(count);
		pageCommonResultDto.setOffset(offset);


		return view("/jsp/member/userList", pageCommonResultDto);
	}

	/**
	 * 跳转到会员新增
	 * 
	 * @return
	 */
	@RequestMapping("/toAdd")
	public ModelAndView toAdd() {
		return view("/jsp/member/memberadd");
	}

	@RequestMapping("/add")
	public @ResponseBody CommonResultJsonDto add(UserDto dto) {
		CommonResultJsonDto result = CommonResultJsonDto.success("成功哦！！");
		try {
			User user = UserDto.toUser(dto);
			user.setId(UUID.randomUUID().toString());

			user = userApplication.register(user);
			dto = UserDto.toDto(user);

		} catch (DuplicatedMobileException ex) {
			ex.printStackTrace();
			result = CommonResultJsonDto.fail("电话号码重复！！");
		} catch (DuplicatedUserNameException ex) {
			ex.printStackTrace();
			result = CommonResultJsonDto.fail("名字重复！！");
		} catch (Exception ex) {
			ex.printStackTrace();
			result = CommonResultJsonDto.fail(ex.getMessage());
		}

		return result;
	}

	@RequestMapping("/toUpdate/{id}")
	public ModelAndView toUpdate(@PathVariable("id") String id) {

		User userById = userApplication.getUserById(id);
		UserDto userDto = UserDto.toDto(userById);
		return view("/jsp/member/memberupdate", userDto);
	}

	@RequestMapping("/update")
	public @ResponseBody CommonResultJsonDto update(UserDto dto) {
		CommonResultJsonDto result = CommonResultJsonDto.success("修改成功");
		try {
			User user = UserDto.toUser(dto);
			User user1 = userApplication.updateUser(user);
		} catch (DuplicatedMobileException ex) {
			ex.printStackTrace();
			result = CommonResultJsonDto.fail("电话号码重复");
		} catch (DuplicatedUserNameException ex) {
			ex.printStackTrace();
			result = CommonResultJsonDto.fail("名字重复");
		} catch (Exception ex) {
			ex.printStackTrace();
			result = CommonResultJsonDto.fail(ex.getMessage());
		}
		return result;
	}

	@RequestMapping("/listAuthentication")
	public ModelAndView listAuthentication(UserQueryDto dto) {
		List<User> list = userApplication.findAuthenWherePage(dto);
		return view("/jsp/member/listAuthentication", list);
	}

	@RequestMapping("userDetail")
	public ModelAndView userDetail(String id) {
		User user = userApplication.getUserById(id);
		return view("/jsp/member/userDetail", user);
	}
}
